#include "global.h"
#include "client_socket.h"

int ClientSocket(const char *host, int port)
{
    int sock;
    in_addr_t inaddr;
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    inaddr = inet_addr(host);
    if (inaddr != INADDR_NONE) // 是 32 全是 1, 即为广播地址 255.255.255.255
    {
        server_addr.sin_addr.s_addr = inaddr;
    }
    else
    {
        return -1;
    }

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0)
    {
        return sock;
    }

    if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        return -1;
    }
    return sock;
}
